<?php
/**
 * IG_Menu
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Menu.php 586 2011-05-31 10:07:18Z mmxie $
 */
class IG_Menu
{
    protected $_cacheKey = 'lvan_menu';

    /**
     *
     * @var Zend_Cache
     */
    protected $_cache;

    protected $_menuList = array();

    protected $_filters = array();

    protected $_moduleName = 'system';

    /**
     *
     * @var System_Model
     */
    protected $_identity;

    public function __construct($moduleName = null)
    {
        if ($moduleName !== null) {
            $this->_moduleName = $moduleName;
        }
        /**
         * @var Zend_Cache
         */
        $cache = Zend_Registry::get('lvan_cache');
        if (!CACHE_ENABLE || null == ($this->_menuList = $cache->load($this->_cacheKey))) {
            $menu = new System_Model_Menu();
            $list = $menu->fetchAll();
            if (!empty($list)) {
                $this->_menuList = $this->buildMenuTree(0, $list);
                unset($list);
                if (CACHE_ENABLE) {
                    $cache->save($this->_menuList, $this->_cacheKey);
                }
            }
        }
    }

    /**
     * 过滤菜单后，返回当前用户所能查看的菜单
     *
     * @return array
     */
    public function current()
    {
        if (!isset($this->_menuList[$this->_moduleName])) {
            return array();
        }
        $tree = array($this->_menuList[$this->_moduleName]);
        return array_filter($tree, array($this, 'filter'));
    }

    public function getMenuTree()
    {
        return $this->_menuList;
    }

    /**
     * 添加过滤器
     *
     * @param IG_Menu_Filter $filter
     */
    public function addFilter($filter)
    {
        if ($filter instanceof IG_Menu_Filter_Interface) {
            $this->_filters[] = $filter;
        } else {
            throw new IG_Menu_Exception('Invalid filter provided');
        }
    }

    /**
     * 过滤菜单
     *
     * @param IG_Menu_Item $item
     * @return boolean
     */
    public function filter($item)
    {
        foreach ($this->_filters as $filter) {
            if (!$filter->accept($item)) {
                return false;
            }
        }
        if ($item instanceof IG_Menu_Item && $item->hasSubmenu()) {
            $submenu = array_filter($item->getSubmenu(), array($this, 'filter'));
            $item->setSubmenu($submenu);
        }
        return true;
    }

    /**
     * 根据System中的Menu Model，来创建树形菜单
     *
     * @param $parentId    父菜单ID
     * @param $list        Menu列表
     * @return array
     */
    public function buildMenuTree($parentId, $list)
    {
        $items = array();
        foreach ($list as $row) {
            if ($row->getParentId() == $parentId) {
                $options = array(
                    'id' => $row->getMenuId(),
                    'module' => $row->getModuleName(),
                    'title' => $row->getName(),
                    'color' => $row->getColor(),
                    'url' => $row->getUrl(),
                    'target' => $row->getTarget()
                );
                $item = new IG_Menu_Item($options);
                foreach ($this->buildMenuTree($row->getMenuId(), $list) as $subitem) {
                    $item->addSubmenu($subitem);
                }
                if ($parentId == 0) {
                    $items[$row->getModuleName()] = $item;
                } else {
                    $items[] = $item;
                }
            }
        }
        return $items;
    }
}